<?php
//  ------------------------------------------------------------------------ //
//                XOOPS - PHP Content Management System                      //
//                    Copyright (c) 2000 xoops.org                           //
//                       <http://www.xoops.org/>                             //
//  ------------------------------------------------------------------------ //
//  This program is free software; you can redistribute it and/or modify     //
//  it under the terms of the GNU General Public License as published by     //
//  the Free Software Foundation; either version 2 of the License, or        //
//  (at your option) any later version.                                      //
//                                                                           //
//  You may not change or alter any portion of this comment or credits       //
//  of supporting developers from this source code or any supporting         //
//  source code which is considered copyrighted (c) material of the          //
//  original comment or credit authors.                                      //
//                                                                           //
//  This program is distributed in the hope that it will be useful,          //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
//  GNU General Public License for more details.                             //
//                                                                           //
//  You should have received a copy of the GNU General Public License        //
//  along with this program; if not, write to the Free Software              //
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
//  ------------------------------------------------------------------------ //
// Author: Dirk Herrmann (Alfred)                                            //
// Site: http://www.simple-xoops.de                                          //
// Project: The XOOPS Project                                                //
// ------------------------------------------------------------------------- //
include "admin_header.php";
$op = (isset($_POST['op'])) ? $_POST['op'] : ((isset($_GET['op'])) ? $_GET['op'] : "show");
if ($op=="save" && !isset($_POST['post'])) $op="editit";
$id = (isset($_POST['id'])) ? intval($_POST['id']) : ((isset($_GET['id'])) ? intval($_GET['id']) : 0);
$catid = $cat = (isset($_GET['cat'])) ? intval($_GET['cat']) : ((isset($_POST['cat'])) ? intval($_POST['cat']) : 1);
$groupid = (isset($_GET['groupid'])) ? intval($_GET['groupid']) : ((isset($_POST['groupid'])) ? intval($_POST['groupid']) : 0);
$mod_isAdmin = ($xoopsUser && $xoopsUser->isAdmin()) ? true : false;
$module_name = $xoopsModule->getVar("dirname");
switch ($op) {
  case "approved":
	  xoops_cp_header();
	  InfoAdminMenu(5);
	  $info = new infosite();
	  $info->getListApproved();
	  xoops_cp_footer();
	  break;
	case "deletehp":
	  $sql="UPDATE ".$xoopsDB->prefix($xoopsModule->getVar('dirname'))." SET frontpage=0 WHERE frontpage>0";
	  $result=$xoopsDB->query($sql);
	  redirect_header("index.php?cat=$catid", 3, _INFO_DBUPDATED);
	  exit();
	  break;
	case "app_edit":
	  xoops_cp_header();
	  InfoAdminMenu(9);
      $info = new infosite();
	  $info->load_app($id);
	  $info->getform();
	  xoops_cp_footer();
	  break;
	case "edit":
	  xoops_cp_header();
	  InfoAdminMenu(9);
      $info = new infosite();
	  if ($id > 0) $info->load($id);
	  $info->getform();
	  xoops_cp_footer();
	  break;
	case "editit":
	  xoops_cp_header();
	  InfoAdminMenu(9);
      $info = new infosite();
	  $info->getform();
	  xoops_cp_footer();
	  break;
	case "blockcat":
	  xoops_cp_header();
	  InfoAdminMenu(2);
      $blockcat = new blockcategorie();
	  $blockcat->getform_list();
	  $blockcat->getform_edit();
	  xoops_cp_footer();
	  break;
	case "blockcat_list":
	  $blockcat = new blockcategorie();
	  switch ($_POST['post']) {
		  case _EDIT:
			xoops_cp_header();
			InfoAdminMenu(2);
            $blockcat->load(intval($_POST['cat']));
		    $blockcat->getform_edit();
		    xoops_cp_footer(); 
			break;
		  case _DELETE:
			xoops_cp_header();
			InfoAdminMenu(2);
            $blockcat->load(intval($_POST['cat']));
		    $blockcat->getform_delete();
		    xoops_cp_footer(); 
			break;
		}
		break;
	case "save":
	    if (!$GLOBALS['xoopsSecurity']->check()) {
          redirect_header("index.php",3,implode('<br />', $GLOBALS['xoopsSecurity']->getErrors()));
          exit;
        }
	    $info = new infosite();
		$approve=0;
		$infothisgroups = (is_object($xoopsUser)) ? $xoopsUser->getGroups() : array(XOOPS_GROUP_ANONYMOUS);
		$infoperm_handler = xoops_gethandler('groupperm');
		$show_info_perm = $infoperm_handler->getItemIds('InfoPerm', $infothisgroups, $xoopsModule->getVar('mid'));
		if (in_array(_CON_INFO_CANFREEALL,$show_info_perm)) $approve=1;
		if ($xoopsUser && $xoopsUser->uid()==$info->getVar('owner') || $mod_isAdmin) { // eigene Seite oder Admin
		  if (in_array(_CON_INFO_CANUPDATE_SITEFULL,$show_info_perm) || $mod_isAdmin) $approve=1;
		  if ($info->is_new() && in_array(24,$show_info_perm) || $mod_isAdmin) $approve=1;
		}
		if ($approve==1) {
		  if (!$info->insert()) {
		    xoops_cp_header();
			InfoAdminMenu(3);
            echo $info->getHtmlErrors();
	        $info->getform();
		    xoops_cp_footer();
		  } else {
			  if (!empty($_POST['freigabe'])) {
				  $sql="DELETE FROM ".$xoopsDB->prefix($xoopsModule->getVar('dirname')."_bak")." WHERE bakid=".$id;
				  $result=$xoopsDB->queryF($sql);
				}
		    redirect_header("index.php?cat=$catid", 3, _INFO_DBUPDATED);
	      exit();
			}
		} else {
		  if (!$info->backup()) {
		    xoops_cp_header();
			InfoAdminMenu(3);
            echo $info->getHtmlErrors();
	        $info->getform();
		    xoops_cp_footer();
		  } else {
		    redirect_header("index.php?cat=$catid", 3, _INFO_DBUPDATED);
	      exit();
			}
		}
	  break;
	case "delete":
	  if ($id > 0) {
		  $info = new infosite();
			xoops_cp_header();
			InfoAdminMenu(3);
      $info->load($id);
		  $info->getform_delete();
		  xoops_cp_footer();	
		}
		break;
	case "info_delete":
	  if ($id > 0) {
		  switch ($_POST['post']) {
		    case _YES:
			    $sql="DELETE FROM ".$xoopsDB->prefix($xoopsModule->getVar('dirname'))." WHERE storyid=".$id;
				  $result=$xoopsDB->query($sql);
				  redirect_header("index.php?cat=$catid", 3, _INFO_DBUPDATED);
	        exit();
		      break;
			  case _NO:
			   redirect_header("index.php?cat=$catid", 3, _INFO_DELFLUSH);
	       exit();
			   break;
		  }
		}
		break;
	case "app_delete":
	  if ($id > 0) {
		  $info = new infosite();
			xoops_cp_header();
			InfoAdminMenu(3);
      $info->load_app($id);
		  $info->getform_app_delete();
		  xoops_cp_footer();	
		}
		break;
	case "app_info_delete":
	  if ($id > 0) {
		  switch ($_POST['post']) {
		    case _YES:
			    $sql="DELETE FROM ".$xoopsDB->prefix($xoopsModule->getVar('dirname')."_bak")." WHERE bakid=".$id;
				  $result=$xoopsDB->query($sql);
				  redirect_header("index.php?op=approved", 3, _INFO_DBUPDATED);
	        exit();
		      break;
			  case _NO:
				default:
			   redirect_header("index.php?op=approved", 3, _INFO_DELFLUSH);
	       exit();
			   break;
		  }
		}
		break;
	case "blockcat_insert":
	  $blockcat = new blockcategorie();
		if ($blockcat->insert($blockcat) > 0) {
		  xoops_cp_header();
			InfoAdminMenu(2);
      echo $blockcat->getHtmlErrors();
	    $blockcat->getform_edit();
		  xoops_cp_footer();
		} else {
		  redirect_header("index.php?op=blockcat", 3, _INFO_DBUPDATED);
	    exit();
		}
	  break;
	case "blockcat_delete":
	  $blockcat = new blockcategorie();
		if ($blockcat->getVar('catid') == 1) {
		  redirect_header("index.php?op=blockcat", 3, _INFO_ERROR_NODEFAULT);
		  exit();
		}
		switch ($_POST['post']) {
		  case _YES:
			  $sql="DELETE FROM ".$xoopsDB->prefix($xoopsModule->getVar('dirname'))." WHERE homepage=".intval($blockcat->getVar('catid'));
				$result=$xoopsDB->query($sql);
				$sql="DELETE FROM ".$xoopsDB->prefix($xoopsModule->getVar('dirname')."_cat")." WHERE catid=".intval($blockcat->getVar('catid'));
				$result=$xoopsDB->query($sql);
			  redirect_header("index.php?op=blockcat", 3, _INFO_DBUPDATED);
	      exit();
		    break;
			case _NO:
			 redirect_header("index.php?op=blockcat", 3, _INFO_DELFLUSH);
	     exit();
			 break;
		}
		break;
	case "update":
	  if (isset($_POST['id'])) {
		  $id = $_POST['id'];
			$parent_id = isset($_POST['parent_id']) ? $_POST['parent_id'] : array();
			$blockid = $_POST['blockid'];
			$visible = $_POST['visible'];
			$hp = (isset($_POST['hp'][0])) ? intval($_POST['hp']) : 0;
			$fp = (isset($_POST['fp'][0])) ? intval($_POST['fp'][0]) : 0;
			$nocomments = $_POST['nocomments'];
			$submenu = $_POST['submenu'];
			foreach ($id as $storyid) {
			  if (intval($storyid) > 0) {
				  $fpp = ($storyid==$fp) ? 1:0;
					if ($fpp==1) {
					  $sql="UPDATE ".$xoopsDB->prefix($xoopsModule->getVar('dirname'))." SET frontpage=0 WHERE frontpage>0";
		        $result=$xoopsDB->query($sql);
					}
					if (!isset($parent_id[$storyid])) $parent_id[$storyid]=0;
				  $sql="UPDATE ".$xoopsDB->prefix($xoopsModule->getVar('dirname'))." SET ";
				  $sql.="parent_id=" . intval($parent_id[$storyid]) . ",";
					$sql.="blockid=".intval($blockid[$storyid]).",";
					$sql.="visible=".intval($visible[$storyid]).",";
					$sql.="homepage=".$hp.",";
					$sql.="nocomments=".intval($nocomments[$storyid]).",";
					$sql.="submenu=".intval($submenu[$storyid]).",";
					$sql.="frontpage=".$fpp."";
					$sql.=" WHERE storyid='".intval($storyid)."'";
					if ( !$result = $xoopsDB->queryF($sql) )	{
				    echo _INFO_ERRORINSERT."<br />[ ".$sql." ]<hr>";
					}
				}
			}
			redirect_header("index.php?cat=$catid",2,_INFO_DBUPDATED);
			exit();
	  } else {
		  redirect_header("index.php?cat=$catid",2,_INFO_DBUPDATED);
		  exit();
	  }
	  break;
	case "show":
	default:
	  xoops_cp_header();
	  InfoAdminMenu(1);
	  $info = new infosite();
	  $info->getList($catid,$groupid);
	  xoops_cp_footer();
	  break;
}
?>
